#!/usr/bin/php
<?php
require("/http/matchtest/classes/class.phpmailer.php");
// CRONTAB ENTRY:  /http/matchtest/tests/daily_bt_traffic_generation.php

error_reporting(E_ERROR | E_PARSE);

// set start time
$time_start = microtime(true);
$debug=1;
$output = "";

$testtime = time();
$i_current_time = $testtime;
$current_time = date('h:i:s a', $testtime);
$output = $output."\nCurrent time is: ".$current_time."\n";
$today = date('F j Y', $testtime);
$readabledate = date("D, M j H:i:s",$testtime);

$day_of_the_week = date('l', $testtime);
// $day_of_the_week = "Sunday";

$output = $output."The current day of the week is: ".$day_of_the_week."\n";
$status1 = "pass";$status2 = "pass";$status3 = "pass";$status4 = "pass";$status5 = "pass";$status6 = "pass";$status7 = "pass";
$nolog = "";

//Test URLs
$url1 = "http://crtldev.aimatch.com:60000/canary/hserver/mid=111111111/site=TRAIT1/AREA=TRAIT1/size=468x60/BTVisitor=1".$nolog;
$url2 = "http://crtldev.aimatch.com:60000/canary/hserver/mid=222222222/site=TRAIT2/AREA=TRAIT2/size=468x60/BTVisitor=2".$nolog;
$url3 = "http://crtldev.aimatch.com:60000/canary/hserver/mid=333333333/site=TRAIT3/AREA=TRAIT3/size=468x60/BTVisitor=3".$nolog;
$url4 = "http://crtldev.aimatch.com:60000/canary/hserver/mid=444444444/site=TRAIT4/AREA=TRAIT4/size=468x60/BTVisitor=4".$nolog;
$url5 = "http://crtldev.aimatch.com:60000/canary/hserver/mid=555555555/site=TRAIT5/AREA=TRAIT5/size=468x60/BTVisitor=5".$nolog;
$url6 = "http://crtldev.aimatch.com:60000/canary/hserver/mid=666666666/site=TRAIT6/AREA=TRAIT6/size=468x60/BTVisitor=6".$nolog;
$url7 = "http://crtldev.aimatch.com:60000/canary/hserver/mid=777777777/site=TRAIT7/AREA=TRAIT7/size=468x60/BTVisitor=7".$nolog;



if($day_of_the_week == "Monday"){
	$output = $output."Executing all of the Monday ad calls.\n";
	// Visitor 1 - Three impressions for SITE=TRAIT1 and AREA=TRAIT1
	$output = $output."--------------------\n";
	$output = $output."Visitor 1 - Three impressions for SITE=TRAIT1 and AREA=TRAIT1\n";
	$i=0;
	while($i < 3){
		$output1 = MakeAdCall($url1);
		$output = $output."ADCALL: ".$url1."\n";
//		echo "OUTPUT: ".$output1."\n";
		$status1 = CheckResponse($output1,"111111111.jpg");
	$output = $output."STATUS: ".$status1."\n";
		$i++;
	}
	$i=0;
	// Visitor 2 - No Impressions 
	$output = $output."--------------------\n";
	$output = $output."Visitor 2 - No Impressions\n";
	// Visitor 3 - Three impressions for SITE=TRAIT3 and AREA=TRAIT3 
	$output = $output."--------------------\n";
	$output = $output."Visitor 3 - Three impressions for SITE=TRAIT3 and AREA=TRAIT3\n";
	$i=0;
	while($i < 3){
		$output3 = MakeAdCall($url3);
	$output = $output."ADCALL: ".$url3."\n";
//		echo "OUTPUT: ".$output3."\n";
		$status3 = CheckResponse($output3,"333333333.jpg");
	$output = $output."STATUS: ".$status3."\n";
		$i++;
	}
	$i=0;
	// Visitor 4 - No Impressions 
	$output = $output."--------------------\n";
	$output = $output."Visitor 4 - No Impressions\n";
	// Visitor 5 - Three impressions for SITE=TRAIT5 and AREA=TRAIT5 
	$output = $output."--------------------\n";
	$output = $output."Visitor 5 - Three impressions for SITE=TRAIT5 and AREA=TRAIT5\n";
	$i=0;
	while($i < 3){
		$output5 = MakeAdCall($url5);
	$output = $output."ADCALL: ".$url5."\n";
//		echo "OUTPUT: ".$output5."\n";
		$status5 = CheckResponse($output5,"555555555.jpg");
	$output = $output."STATUS: ".$status5."\n";
		$i++;
	}
	$i=0;
	// Visitor 6 - One impression logged for SITE=TRAIT6 and AREA=TRAIT6
	$output = $output."--------------------\n";
	$output = $output."Visitor 6 - One impression logged for SITE=TRAIT6 and AREA=TRAIT6\n";
		$output6 = MakeAdCall($url6);
	$output = $output."ADCALL: ".$url6."\n";
//		echo "OUTPUT: ".$output6."\n";
		$status6 = CheckResponse($output6,"666666666.jpg");
	$output = $output."STATUS: ".$status6."\n";
	// Visitor 7 - No Impressions
	echo "--------------------\n";
	echo "Visitor 7 - No Impressions\n";
}

else if($day_of_the_week == "Tuesday"){
	$output = $output."Executing all of the Tuesday ad calls.\n";
	// Visitor 1 - Three impressions for SITE=TRAIT1 and AREA=TRAIT1 
	$output = $output."--------------------\n";
	$output = $output."Visitor 1 - Three impressions for SITE=TRAIT1 and AREA=TRAIT1\n";
	$i=0;
	while($i < 3){
		$output1 = MakeAdCall($url1);
	$output = $output."ADCALL: ".$url1."\n";
//		echo "OUTPUT: ".$output1."\n";
		$status1 = CheckResponse($output1,"111111111.jpg");
	$output = $output."STATUS: ".$status1."\n";
		$i++;
	}
	// Visitor 2 - No Impressions 
	$output = $output."--------------------\n";
	$output = $output."Visitor 2 - No Impressions \n";
	// Visitor 3 - Three impressions for SITE=TRAIT3 and AREA=TRAIT3 
	$output = $output."--------------------\n";
	$output = $output."Visitor 3 - Three impressions for SITE=TRAIT3 and AREA=TRAIT3\n";
	$i=0;
	while($i < 3){
		$output3 = MakeAdCall($url3);
	$output = $output."ADCALL: ".$url3."\n";
//		echo "OUTPUT: ".$output3."\n";
		$status3 = CheckResponse($output3,"333333333.jpg");
	$output = $output."STATUS: ".$status3."\n";
		$i++;
	}
	$i=0;
	// Visitor 4 - No One Impression for SITE=TRAIT4 and AREA=TRAIT4
	$output = $output."--------------------\n";
	$output = $output."Visitor 4 - One Impression for SITE=TRAIT4 and AREA=TRAIT4\n";
		$output4 = MakeAdCall($url4);
	$output = $output."ADCALL: ".$url4."\n";
//		echo "OUTPUT: ".$output4."\n";
		$status4 = CheckResponse($output4,"444444444.jpg");
	$output = $output."STATUS: ".$status4."\n";
	// Visitor 5 - Three impressions for SITE=TRAIT5 and AREA=TRAIT5 
	$output = $output."--------------------\n";
	$output = $output."Visitor 5 - Three impressions for SITE=TRAIT5 and AREA=TRAIT5\n";
	$i=0;
	while($i < 3){
		$output5 = MakeAdCall($url5);
	$output = $output."ADCALL: ".$url5."\n";
//		echo "OUTPUT: ".$output5."\n";
		$status5 = CheckResponse($output5,"555555555.jpg");
	$output = $output."STATUS: ".$status5."\n";
		$i++;
	}
	$i=0;
	// Visitor 6 - One impression logged for SITE=TRAIT6 and AREA=TRAIT6 
	$output = $output."--------------------\n";
	$output = $output."Visitor 6 - One impression logged for SITE=TRAIT6 and AREA=TRAIT6\n";
		$output6 = MakeAdCall($url6);
	$output = $output."ADCALL: ".$url6."\n";
//		echo "OUTPUT: ".$output6."\n";
		$status6 = CheckResponse($output6,"666666666.jpg");
	$output = $output."STATUS: ".$status6."\n";
	// Visitor 7 - No Impressions 
	$output = $output."--------------------\n";
	$output = $output."Visitor 7 - No Impressions\n";
}

else if($day_of_the_week == "Wednesday"){
	$output = $output."Executing all of the Wednesday ad calls.\n";
	// Visitor 1 - No Impressions 
	$output = $output."--------------------\n";
	$output = $output."Visitor 1 - No Impressions\n";
	// Visitor 2 - Three impressions for SITE=TRAIT2 and AREA=TRAIT2
	$output = $output."--------------------\n";
	$output = $output."Visitor 2 - One impression for SITE=TRAIT2 and AREA=TRAIT2\n";
		$output2 = MakeAdCall($url2);
	$output = $output."ADCALL: ".$url2."\n";
//		echo "OUTPUT: ".$output2."\n";
		$status2 = CheckResponse($output2,"222222222.jpg");
	$output = $output."STATUS: ".$status2."\n";	
	// Visitor 3 - No Impressions 
	$output = $output."--------------------\n";
	$output = $output."Visitor 3 - No Impressions\n";
	// Visitor 4 - No Impressions 
	$output = $output."--------------------\n";
	$output = $output."Visitor 4 - No Impressions\n";
	// Visitor 5 - Three impressions for SITE=TRAIT5 and AREA=TRAIT5 
	$output = $output."--------------------\n";
	$output = $output."Visitor 5 - Three impressions for SITE=TRAIT5 and AREA=TRAIT5\n";
	$i=0;
	while($i < 3){
		$output5 = MakeAdCall($url5);
	$output = $output."ADCALL: ".$url5."\n";
//		echo "OUTPUT: ".$output5."\n";
		$status5 = CheckResponse($output5,"555555555.jpg");
	$output = $output."STATUS: ".$status5."\n";
		$i++;
	}
	$i=0;
	// Visitor 6 - One impression logged for SITE=TRAIT6 and AREA=TRAIT6 
	echo "--------------------\n";
	echo "Visitor 6 - One impression logged for SITE=TRAIT6 and AREA=TRAIT6\n";
		$output6 = MakeAdCall($url6);
	$output = $output."ADCALL: ".$url6."\n";
//		echo "OUTPUT: ".$output6."\n";
		$status6 = CheckResponse($output6,"666666666.jpg");
	$output = $output."STATUS: ".$status6."\n";
	// Visitor 7 - No Impressions 
	$output = $output."--------------------\n";
	$output = $output."Visitor 7 - No Impressions\n";
}

else if($day_of_the_week == "Thursday"){
	$output = $output."Executing all of the Thursday ad calls.\n";
	// Visitor 1 - One impression logged for SITE=TRAIT1 and AREA=TRAIT1
	$output = $output."--------------------\n";
	$output = $output."Visitor 1 - No Impressions\n";
	// Visitor 2 - No Impressions 
	$output = $output."--------------------\n";
	$output = $output."Visitor 2 - No Impressions\n";
	// Visitor 3 - No Impressions
	$output = $output."--------------------\n";
	$output = $output."Visitor 3 - No Impressions\n";
	// Visitor 4 - Three impressions for SITE=TRAIT4 and AREA=TRAIT4 
	$output = $output."--------------------\n";
	$output = $output."Visitor 4 - Three impressions for SITE=TRAIT4 and AREA=TRAIT4\n";
	$i=0;
	while($i < 3){
		$output4 = MakeAdCall($url4);
	$output = $output."ADCALL: ".$url4."\n";
//		echo "OUTPUT: ".$output4."\n";
		$status4 = CheckResponse($output4,"444444444.jpg");
	$output = $output."STATUS: ".$status4."\n";
		$i++;
	}
	$i=0;
	// Visitor 5 - Three impressions for SITE=TRAIT5 and AREA=TRAIT5 
	$output = $output."--------------------\n";
	$output = $output."Visitor 5 - Three impressions for SITE=TRAIT5 and AREA=TRAIT5 \n";
	$i=0;
	while($i < 3){
		$output5 = MakeAdCall($url5);
	$output = $output."ADCALL: ".$url5."\n";
//		echo "OUTPUT: ".$output5."\n";
		$status5 = CheckResponse($output5,"555555555.jpg");
	$output = $output."STATUS: ".$status5."\n";
		$i++;
	}
	$i=0;
	// Visitor 6 - One impression logged for SITE=TRAIT6 and AREA=TRAIT6 
	$output = $output."--------------------\n";
	$output = $output."Visitor 6 - One impression logged for SITE=TRAIT6 and AREA=TRAIT6 \n";
		$output6 = MakeAdCall($url6);
	$output = $output."ADCALL: ".$url6."\n";
//		echo "OUTPUT: ".$output6."\n";
		$status6 = CheckResponse($output6,"666666666.jpg");
	$output = $output."STATUS: ".$status6."\n";
	// Visitor 7 - No Impressions 
	$output = $output."--------------------\n";
	$output = $output."Visitor 7 - No Impressions\n";
}

else if($day_of_the_week == "Friday"){
	$output = $output."Executing all of the Friday ad calls.\n";
	// Visitor 1 - One impression logged for SITE=TRAIT1 and AREA=TRAIT1
	$output = $output."--------------------\n";
	$output = $output."Visitor 1 - One impression logged for SITE=TRAIT1 and AREA=TRAIT1\n";
		$output1 = MakeAdCall($url1);
	$output = $output."ADCALL: ".$url1."\n";
//		echo "OUTPUT: ".$output1."\n";
		$status1 = CheckResponse($output1,"111111111.jpg");
	$output = $output."STATUS: ".$status1."\n";	
	// Visitor 2 - Three impressions for SITE=TRAIT2 and AREA=TRAIT2 
	$output = $output."--------------------\n";
	$output = $output."Visitor 2 - Three impressions for SITE=TRAIT2 and AREA=TRAIT2\n";
	$i=0;
	while($i < 3){
		$output2 = MakeAdCall($url2);
	$output = $output."ADCALL: ".$url2."\n";
//		echo "OUTPUT: ".$output2."\n";
		$status2 = CheckResponse($output2,"222222222.jpg");
	$output = $output."STATUS: ".$status2."\n";
		$i++;
	}
	$i=0;
	// Visitor 3 - No Impressions 
	$output = $output."--------------------\n";
	$output = $output."Visitor 3 - One impression logged for SITE=TRAIT3 and AREA=TRAIT3\n";
		$output3 = MakeAdCall($url3);
	$output = $output."ADCALL: ".$url3."\n";
//		echo "OUTPUT: ".$output3."\n";
		$status3 = CheckResponse($output3,"333333333.jpg");
	$output = $output."STATUS: ".$status3."\n";	
	// Visitor 4 - Three impressions for SITE=TRAIT4 and AREA=TRAIT4 
	$output = $output."--------------------\n";
	$output = $output."Visitor 4 - Three impressions for SITE=TRAIT4 and AREA=TRAIT4\n";
	$i=0;
	while($i < 3){
		$output4 = MakeAdCall($url4);
	$output = $output."ADCALL: ".$url4."\n";
//		echo "OUTPUT: ".$output4."\n";
		$status4 = CheckResponse($output4,"444444444.jpg");
	$output = $output."STATUS: ".$status4."\n";
		$i++;
	}
	$i=0;
	// Visitor 5 - Three impressions for SITE=TRAIT5 and AREA=TRAIT5 
	$output = $output."--------------------\n";
	$output = $output."Visitor 5 - Three impressions for SITE=TRAIT5 and AREA=TRAIT5\n";
	$i=0;
	while($i < 3){
		$output5 = MakeAdCall($url5);
	$output = $output."ADCALL: ".$url5."\n";
//		echo "OUTPUT: ".$output5."\n";
		$status5 = CheckResponse($output5,"555555555.jpg");
	$output = $output."STATUS: ".$status5."\n";
		$i++;
	}
	$i=0;
	// Visitor 6 - One impression logged for SITE=TRAIT6 and AREA=TRAIT6 
	$output = $output."--------------------\n";
	$output = $output."Visitor 6 - One impression logged for SITE=TRAIT6 and AREA=TRAIT6\n";
		$output6 = MakeAdCall($url6);
	$output = $output."ADCALL: ".$url6."\n";
//		echo "OUTPUT: ".$output6."\n";
		$status6 = CheckResponse($output6,"666666666.jpg");
	$output = $output."STATUS: ".$status6."\n";
	// Visitor 7 - No Impressions 
	$output = $output."--------------------\n";
	$output = $output."Visitor 7 - No Impressions\n";
}

else if($day_of_the_week == "Saturday"){
	$output = $output."Executing all of the Saturday ad calls.\n";
	// Visitor 1 - No Impressions 
	$output = $output."--------------------\n";
	$output = $output."Visitor 1 - No Impressions\n";
	// Visitor 2 - Three impressions for SITE=TRAIT2 and AREA=TRAIT2 
	$output = $output."--------------------\n";
	$output = $output."Visitor 2 - Three impressions for SITE=TRAIT2 and AREA=TRAIT2\n";
	$i=0;
	while($i < 3){
		$output2 = MakeAdCall($url2);
	$output = $output."ADCALL: ".$url2."\n";
//		echo "OUTPUT: ".$output2."\n";
		$status2 = CheckResponse($output2,"222222222.jpg");
	$output = $output."STATUS: ".$status2."\n";
		$i++;
	}
	$i=0;
	// Visitor 3 - No Impressions 
	$output = $output."--------------------\n";
	$output = $output."Visitor 3 - No Impressions \n";
	// Visitor 4 - Three impressions for SITE=TRAIT4 and AREA=TRAIT4 
	$output = $output."--------------------\n";
	$output = $output."Visitor 4 - Three impressions for SITE=TRAIT4 and AREA=TRAIT4\n";
	$i=0;
	while($i < 3){
		$output4 = MakeAdCall($url4);
	$output = $output."ADCALL: ".$url4."\n";
//		echo "OUTPUT: ".$output4."\n";
		$status4 = CheckResponse($output4,"444444444.jpg");
	$output = $output."STATUS: ".$status4."\n";
		$i++;
	}
	$i=0;
	// Visitor 5 - Three impressions for SITE=TRAIT5 and AREA=TRAIT5 
	$output = $output."--------------------\n";
	$output = $output."Visitor 5 - Three impressions for SITE=TRAIT5 and AREA=TRAIT5\n";
	$i=0;
	while($i < 3){
		$output5 = MakeAdCall($url5);
	$output = $output."ADCALL: ".$url5."\n";
//		echo "OUTPUT: ".$output5."\n";
		$status5 = CheckResponse($output5,"555555555.jpg");
	$output = $output."STATUS: ".$status5."\n";
		$i++;
	}
	$i=0;
	// Visitor 6 - One impression logged for SITE=TRAIT6 and AREA=TRAIT6 
	$output = $output."--------------------\n";
	$output = $output."Visitor 6 - One impression logged for SITE=TRAIT6 and AREA=TRAIT6\n";
		$output6 = MakeAdCall($url6);
	$output = $output."ADCALL: ".$url6."\n";
//		echo "OUTPUT: ".$output6."\n";
		$status6 = CheckResponse($output6,"666666666.jpg");
	$output = $output."STATUS: ".$status6."\n";
	// Visitor 7 - No Impressions 
	$output = $output."--------------------\n";
	$output = $output."Visitor 7 - No Impressions\n";
}

else if($day_of_the_week == "Sunday"){
	$output = $output."Executing all of the Sunday ad calls.\n";
	// Visitor 1 - Three impressions for SITE=TRAIT1 and AREA=TRAIT1 
	$output = $output."--------------------\n";
	$output = $output."Visitor 1 - Three impressions for SITE=TRAIT1 and AREA=TRAIT1\n";
	$i=0;
	while($i < 3){
		$output1 = MakeAdCall($url1);
	$output = $output."ADCALL: ".$url1."\n";
//		echo "OUTPUT: ".$output1."\n";
		$status1 = CheckResponse($output1,"111111111.jpg");
	$output = $output."STATUS: ".$status1."\n";
		$i++;
	}
	$i=0;
	// Visitor 2 - Three impressions for SITE=TRAIT2 and AREA=TRAIT2
	$output = $output."--------------------\n";
	$output = $output."Visitor 2 - Three impressions for SITE=TRAIT2 and AREA=TRAIT2\n";
	$i=0;
	while($i < 3){
		$output2 = MakeAdCall($url2);
	$output = $output."ADCALL: ".$url2."\n";
//		echo "OUTPUT: ".$output2."\n";
		$status2 = CheckResponse($output2,"222222222.jpg");
	$output = $output."STATUS: ".$status2."\n";
		$i++;
	}
	$i=0;
	// Visitor 3 - Three impressions for SITE=TRAIT3 and AREA=TRAIT3 
	$output = $output."--------------------\n";
	$output = $output."Visitor 3 - Three impressions for SITE=TRAIT3 and AREA=TRAIT3\n";
	$i=0;
	while($i < 3){
		$output3 = MakeAdCall($url3);
	$output = $output."ADCALL: ".$url3."\n";
//		echo "OUTPUT: ".$output3."\n";
		$status3 = CheckResponse($output3,"333333333.jpg");
	$output = $output."STATUS: ".$status3."\n";
		$i++;
	}
	$i=0;
	// Visitor 4 - No Impressions 
	$output = $output."--------------------\n";
	$output = $output."Visitor 4 - No Impressions\n";
	// Visitor 5 - Three impressions for SITE=TRAIT5 and AREA=TRAIT5 
	$output = $output."--------------------\n";
	$output = $output."Visitor 5 - Three impressions for SITE=TRAIT5 and AREA=TRAIT5\n";
	$i=0;
	while($i < 3){
		$output5 = MakeAdCall($url5);
	$output = $output."ADCALL: ".$url5."\n";
//		echo "OUTPUT: ".$output5."\n";
		$status5 = CheckResponse($output5,"555555555.jpg");
	$output = $output."STATUS: ".$status5."\n";
		$i++;
	}
	$i=0;
	// Visitor 6 - One impression logged for SITE=TRAIT6 and AREA=TRAIT6 
	$output = $output."--------------------\n";
	$output = $output."Visitor 6 - One impression logged for SITE=TRAIT6 and AREA=TRAIT6\n";
		$output6 = MakeAdCall($url6);
	$output = $output."ADCALL: ".$url6."\n";
//		echo "OUTPUT: ".$output6."\n";
		$status6 = CheckResponse($output6,"666666666.jpg");
	$output = $output."STATUS: ".$status6."\n";
	// Visitor 7 - No Impressions 
	$output = $output."--------------------\n";
	$output = $output."Visitor 7 - One impression logged for SITE=TRAIT7 and AREA=TRAIT7\n";
		$output7 = MakeAdCall($url7);
	$output = $output."ADCALL: ".$url7."\n";
//		echo "OUTPUT: ".$output7."\n";
		$status7 = CheckResponse($output7,"777777777.jpg");
	$output = $output."STATUS: ".$status7."\n";
}


$time_end = microtime(true);
$total_time = $time_end - $time_start;

	$emailbody = "<font face=\"Tahoma\" size=\"2\">\n";
	$emailbody = $emailbody."<pre>".$output."</pre>\n";

	$EmailStatus = "PASSED";
	if(($status1 == "fail")||($status2 == "fail")||($status3 == "fail")||($status4 == "fail")||($status5 == "fail")||($status6 == "fail")||($status7 == "fail")){
		$EmailStatus = "FAILED"; }
		
	
	if($web){
		echo "<pre>".$output."</pre>"; }
	else {
		echo $output; }

	
	    $mail = new phpmailer();

        $mail->IsSendmail();                                      // set mailer to use SMTP
        $mail->Host = "smtp.gmail.com";       
        $mail->From = "scriptserver@aimatch.com";
        $mail->FromName = "Canary Testing";
        $mail->SMTPAuth = "true";
        $mail->Username = "aimatchrobc@gmail.com";
        $mail->Password = "@1m@tchr0bc";
        $mail->AddAddress("rob@aimatch.com");
        $mail->AddReplyTo("rob@aimatch.com", "Robert Cox");

        $mail->WordWrap = 65;                                 // set word wrap to 65 characters
        $mail->IsHTML(true);                                  // set email format to HTML

        
        $mail->Subject = "BT TRAIT GENERATION: ".$EmailStatus." on ".$readabledate;
        $mail->Body    = $emailbody;

        if(!$mail->Send())
        {
            echo "Message could not be sent. <p>";
            echo "Mailer Error: " . $mail->ErrorInfo;
            exit;
         }


//================= Functions =================
	
function MakeAdCall($url) {
	$UserAgent="Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.16) Gecko/20110319 Firefox/3.6.16";

    // initilize the curl call
    $ch = curl_init();
    // set useragent
    curl_setopt($ch, CURLOPT_USERAGENT, $UserAgent);
    // Set curl to use $url for http call
    curl_setopt($ch, CURLOPT_URL, $url);
    // show client headers
    curl_setopt($ch, CURLOPT_HEADER, 1);
    // have curl put X-Forwarded-For into headers
    curl_setopt($ch,CURLOPT_HTTPHEADER, $client_headers);
    // store results as the return of curl_exec
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    // Follow redirects
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);		
    // if HTML error 400 over over, fail
    curl_setopt($ch, CURLOPT_FAILONERROR, TRUE);

    $output = curl_exec($ch);
    if (isset($debug)) { echo "<br><pre>HTTP Response Output:<br><br>"; print_r($output); echo "</pre><br>"; }
    //close curl connection
    curl_close($ch);

return $output;
}

function MakeOutputArray($output){
        $output_array = preg_split('/[\n]/', $output, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
        if (isset($debug)) { echo "<br><pre>HTTP Response Output (array):<br><br>"; print_r($output_array); echo "</pre><br>"; }

return $output_array;
}

function GetFormattedOutput($output_array, $web,$debug){
        $http_response = array_values(preg_grep("/^HTTP/", $output_array));
        $http_array = explode(" ",$http_response[0]);
        $http_return_code = trim($http_array[1]);
        $http_return_text = trim($http_array[2]);
//        if (isset($debug)) { echo "<br>DEBUG - HTTP CODE/TEXT: $http_return_code / $http_return_text<br>"; }

        /* check code = 200 or 302 and text equal OK, anything else we have an error */

        if (($http_return_text != "OK" || $http_return_code != "200")&&($http_return_text != "Relocate" || $http_return_code != "302")) {
//            if (isset($debug)) { echo "***** ALERT - HTTP CODE/TEXT: ".trim($http_response[0])." *****<br>"; }
				echo "HTTP return code/text was not 200/OK". trim($http_response[0])."<br>";
                die;
        }
		
		// count the number of elements in the response array
		$num_elements = count($output_array);
//        if (isset($debug)) { echo "The Number Of Elements in the OUTPUT Array are: ".$num_elements." <br>"; }
		
		// format the items in the array as a long concatenated string.
		$y=0;
		while($y <= $num_elements){
			$formatted_output = $formatted_output.$output_array[$y]."\n";
			$formatted_output_web = $formatted_output_web.$output_array[$y]."<br>";
			$y++;
		}
//        if (isset($debug)) { echo "The Formatted contents of OUTPUT are: ".$formatted_output_web." *****<br>"; }
		
		if($web) {
			return $formatted_output_web; }
		else {
			return $formatted_output; }
}

function CheckResponse($output,$SearchString){

		if(stripos($output, $SearchString)){
			$status = "pass";
        }
        else {
			$status = "fail";			
		}
	return $status;
}


?>
